package cn.sczhyt.modules.szhy.service.impl;

import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.sys.entity.vo.UserInfoVoEntity;
import cn.sczhyt.modules.szhy.dao.MeetingUserDao;
import cn.sczhyt.modules.szhy.entity.MeetingUserEntity;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;

import cn.sczhyt.modules.szhy.dao.MeetingSignInDao;
import cn.sczhyt.modules.szhy.entity.MeetingSignInEntity;
import cn.sczhyt.modules.szhy.service.MeetingSignInService;
import org.springframework.transaction.annotation.Transactional;


@Service("meetingSignInService")
public class MeetingSignInServiceImpl extends ServiceImpl<MeetingSignInDao, MeetingSignInEntity> implements MeetingSignInService {
    @Autowired
    private MeetingSignInDao meetingSignInDao;
    @Autowired
    private MeetingUserDao meetingUserDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MeetingSignInEntity> page = this.page(
                new Query<MeetingSignInEntity>().getPage(params),
                new QueryWrapper<MeetingSignInEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R signIn(String userId, String username, String meetingId,String signAddress) {
        MeetingSignInEntity meetingSignInEntity = meetingSignInDao.selectBySignIn(userId, username, meetingId);
        if (meetingSignInEntity != null) {
            return R.error(201,"你要干啥子，重复签到了");
        }
        MeetingSignInEntity signInEntity = new MeetingSignInEntity();
        signInEntity.setUserId(userId);
        signInEntity.setUsername(username);
        signInEntity.setMeetingId(meetingId);
        signInEntity.setSignAddress(signAddress);
        signInEntity.setMeetingSignTime(new Date());
        meetingSignInDao.insert(signInEntity);
        return R.ok();

    }



    @Override
    public IPage<MeetingSignInEntity> listByMeetingId(String meetingId, Integer page, Integer limit) {

        Page<MeetingSignInEntity> pages = new Page<>(page,limit);

        return meetingSignInDao.selectByMeetingId(pages,meetingId);
    }

    @Override
    public boolean check(String userId, String username, String meetingId) {

        MeetingSignInEntity meetingSignInEntity = meetingSignInDao.selectBySignIn(userId, username, meetingId);
        if (meetingSignInEntity != null) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<MeetingUserEntity> listNotSignInByMeetingId(String meetingId, Integer page, Integer limit) {


        QueryWrapper<MeetingSignInEntity> queryWrapper = new QueryWrapper<MeetingSignInEntity>().eq("meeting_id",meetingId);

        //拿到所有签到的人员Id
        List<MeetingSignInEntity> list = this.list(queryWrapper);
        List<String> collect = list.stream().map(item -> {
             return item.getUserId();
        }).collect(Collectors.toList());
        //查询时排除以上人员
        System.out.println(collect);
        Page<MeetingUserEntity> pages = new Page<>(page,limit);
        return meetingUserDao.selectNotSignInByMeetingId(pages,meetingId,collect);

    }

}
